package com.tuanzi.loan.web.bean.family.sms;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.service.sms.PhoneLocationService;
import com.tuanzi.loan.business.service.sms.SmsCalculateTaskService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.exception.ValidateException;
import com.tuanzi.loan.vo.request.family.SmsCondition;
import com.tuanzi.loan.vo.request.family.sms.ExternalSmsCalculateCondition;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.xml.bind.ValidationException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.tuanzi.loan.business.constants.DictionaryCode.SMS_EXTERNAL_CHANNEL;
import static com.tuanzi.loan.business.constants.DictionaryCode.SMS_USER_TYPE;

/**
 * @Author: chenweijun
 * @Date: 2018/10/30 17:42
 * @Description:
 */
@Data
@Slf4j
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CalculateExternalSmsBean extends JSFInfoBean<ExternalSmsCalculateCondition> {

    @Autowired
    private PhoneLocationService phoneLocationService;

    @Autowired
    private SmsCalculateTaskService smsCalculateTaskService;

    private Long estimatedSmsCount;
    private Map<String, String> provinces;


    @PostConstruct
    private void init() {
        $ = new ExternalSmsCalculateCondition();
        provinces = phoneLocationService.findDistinctProvince();

    }


    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(SMS_USER_TYPE, SMS_EXTERNAL_CHANNEL);
    }


    /**
     * 计算号码包
     */
    public void onCalculateCount(){

        if(StringUtils.isBlank($.getName())){
            throw new ValidateException("请输入计算任务名称");
        }

        if(StringUtils.isBlank($.getExternalChannel())){
            throw new ValidateException("请选择外部渠道");
        }

        if(Objects.isNull($.getTongDunMinScore()) || Objects.isNull($.getTongDunMaxScore())){
            throw new ValidateException("请输入同盾分");
        }

        if(CollectionUtils.isEmpty($.getTelecomOperators())){
            throw new ValidateException("请选择运营商");
        }

        if(CollectionUtils.isEmpty($.getProvinces())) {
            throw new ValidateException("请选择地区");
        }
        smsCalculateTaskService.addExternalCalculateTask($);
        $ = new ExternalSmsCalculateCondition();
        addInfoMessage("操作成功,任务已加入计算队列");

    }

    /**
     * 预估号码包大小
     */
    public void onEstimate(){


    }



}
